package com.asiainfo.ssm.execute.util;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.rmi.RemoteException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ai.appframe2.common.ServiceManager;
import com.ai.appframe2.complex.cache.CacheFactory;
import com.ai.common.ivalues.IBOBsStaticDataValue;
import com.ai.common.util.ExceptionUtil;
import com.ai.common.util.FtpUtil;
import com.ai.common.util.StaticDataUtil;
import com.asiainfo.appframe.ext.exeframe.ws.client.cache.WsClientCacheImpl;
import com.asiainfo.appframe.ext.exeframe.ws.query.impl.po.CfgWsClient;
import com.asiainfo.ssm.commons.util.FileOperate;
import com.asiainfo.ssm.execute.teaminvoke.out.webservice.util.InstructionServicePublicData;

/**
 * Copyright: Copyright (c) 2014
 * @FileName: ExecUtil.java
 * @Description: ִ�����Ĺ�����
 * @Version: v1.0.0
 * @Author:  yangliu
 * @Date:  2014��8��21��
 * Modification History:
 * Date                   Author           Version            Description
 * ------------------------------------------------------------------------*
 * 2014��8��21��       yangliu            v1.0.0              ����
 */
public class ExecUtil {
	private static transient Log log = LogFactory.getLog(ExecUtil.class);
	 
	public static String[] getFileLists(String ftpPathCode)throws Exception,RemoteException{
		FtpUtil ftp = null;
		String[] lists = null;
		try{
			ftp = new FtpUtil(ftpPathCode);
			
			lists = ftp.list();
 			for(int i=0;(lists==null||lists.length<1)&&i<5;i++){
				lists = ftp.list();
			}
		}catch(Exception e){
			log.error("getFileLists is error=",e);
			throw e;
		}finally{
			if(ftp!=null){
				ftp.close();
				ftp = null;
			}
		}
		if(lists==null){
			ExceptionUtil.throwBusinessException("SOS9234372");
		}
		return lists;
	
	}
	
	public static Timestamp getCurrentDateTs() throws Exception, RemoteException {
		return ServiceManager.getIdGenerator().getSysDate();
	}
	
	 
	public static void removeFile2His(String ftpPathCode,String fileName)throws Exception,RemoteException{
		FtpUtil ftp = null;
		try{
			ftp = new FtpUtil(ftpPathCode);
			ftp.moveFileToRemoteHisDir(fileName);
		
		}catch(Exception e){
			log.error("file remove his is error=",e);
			throw e;
		}finally{
			if(ftp!=null){
				try{
					ftp.close();
				}catch(Exception e){
					log.error(" removeFile2His is error=",e);
					
				}
			}
		}
	}
	
	
	public static CfgWsClient getCfgWsClientByCode(String code)	throws Exception {
		CfgWsClient objCfgWsClient = (CfgWsClient) CacheFactory.get(WsClientCacheImpl.class, "C^" + code);
		return objCfgWsClient;
	}
	
	public static String getTopicId(String actId) throws Exception{
		return ExecInsUtil.getActSequence(actId)+"";
	}
	
	
	public static OutputStream getLocalFileName(String dir,String fileName) throws Exception{
		try {
			FileOperate fileOperate = new FileOperate();
			fileOperate.newFile(dir+"/"+fileName, "");
			return new FileOutputStream(new File(dir+"/"+fileName));
		} catch (Exception ex) {
			ExceptionUtil.throwBusinessException("BAS3000029" + ex.getMessage());
			return null;
		}
	}
	
	public static void uploadFTPFile(String dir,String fileName,String timeFile,String cfgFtpPathCode,String delFileSwitch){
		FtpUtil ftpUtil = null;		
		try {
			ftpUtil = new FtpUtil(cfgFtpPathCode);			
			FileInputStream inputStream= new FileInputStream(dir+"/"+fileName);
			if(!ftpUtil.isExist(timeFile)){
				ftpUtil.mkdir(ftpUtil.getCurrentWorkingDirectory()+"/"+timeFile);
			}			
			ftpUtil.changeWorkingDirectory(ftpUtil.getCurrentWorkingDirectory()+"/"+timeFile);
			
			
			ftpUtil.upload(fileName, inputStream, FtpUtil.BIN);
			inputStream.close(); 
			IBOBsStaticDataValue[] staticDataValue = StaticDataUtil.getStaticData(delFileSwitch);
			if (staticDataValue != null && staticDataValue.length > 0) {
				if ((staticDataValue[0].getCodeValue().trim()).equals((InstructionServicePublicData.DEL_SWITCH_OPEN))) {
					FileOperate operate = new FileOperate();
					operate.deleteFile(dir+"/"+fileName);
				}
			}else{
				FileOperate operate = new FileOperate();
				operate.deleteFile(dir+"/"+fileName);
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		} finally {
			if (ftpUtil != null){
				try {
					ftpUtil.close();
				} catch (Exception e) {
					log.error(e.getMessage());
				}
			}
		}
	}
	
	public static boolean isValidDate(String str) {
		boolean convertSuccess = true;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			format.setLenient(false);
			format.parse(str);
		} catch (ParseException e) { 
			e.printStackTrace();
			convertSuccess = false;
		}
		return convertSuccess;
	}
	
	  
}
